package tuanda.soict.firstsun.AbilityPackage;

import java.util.ArrayList;
import java.util.Random;

import tuanda.soict.firstsun.LogicPackage.Army;
import tuanda.soict.firstsun.LogicPackage.BattleMap;
import tuanda.soict.firstsun.LogicPackage.Coordinate;
import tuanda.soict.firstsun.LogicPackage.Group;
import tuanda.soict.firstsun.LogicPackage.Scenario;

/**
 * @author tuanda_soict_hut
 * @version 0.0.1
 * @since 9-9-2012
 */
public class Lightning_Ability extends Ability {

	final static int LIGHTNING_DAMAGE = 40;

	public Lightning_Ability(int id, String name, int range, int area,
			int affect) {
		super(id, name, range, area, affect);
	}

	@Override
	public void mainAct(Army army, Scenario scenario) {
		int level = getAbilityLevel();
		Coordinate current = army.getCoordinate();
		int range = this.getAbilityRange();
		Coordinate targetCoor = this.getTarget();
		BattleMap map = scenario.getBattleMap();
		Group[] group = scenario.getGroup();
		if (current.getDistance(targetCoor) <= range) {
			ArrayList<Coordinate> affectedArea = calculateAffectedArea(army,
					scenario);
			Random rand = new Random();
			int damage;
			for (Coordinate coor : affectedArea) {
				Army arm = map
						.getArmy(coor.getLocationX(), coor.getLocationY());
				if (arm != null) {
					if (group[arm.getGroupID()].getSide() != group[army
							.getGroupID()].getSide()) {
						int prevArmyHP = army.getHealth();
						int prevEnemyHP = arm.getHealth();

						damage = 10 + (rand.nextInt(LIGHTNING_DAMAGE + level
								* 5));

						int enemyRank = arm.getRank();
						damageToEnemy(army, damage, arm, false);
						if (arm.isInAttackRange(army.getLocationX(),
								army.getLocationY())) {
							Attack enemyAttack = (Attack) arm
									.getAttackAbility();
							enemyAttack.setTarget(current);
							enemyAttack.mainAct(arm, scenario);
							int allyRank = army.getRank();
							int armyDead = prevArmyHP - army.getHealth();
							arm.setExperience(arm.getExperience() + armyDead
									* allyRank, armyManager);
						}
						int enemyDead = prevEnemyHP - arm.getHealth();
						army.setExperience(army.getExperience() + enemyDead
								* enemyRank, armyManager);
					}
				}
			}
		}
	}

	@Override
	public void calculation(Army army, Scenario scenario) {
		Coordinate targetCoor = this.getTarget();
		BattleMap map = scenario.getBattleMap();
		Group[] group = scenario.getGroup();
		Army arm = map.getArmy(targetCoor.getLocationX(),
				targetCoor.getLocationY());
		if (arm != null) {
			if (group[arm.getGroupID()].getSide() != group[army.getGroupID()]
					.getSide()) {
				int damage = LIGHTNING_DAMAGE + (getAbilityLevel() << 1);
				this.setAbilityPoint(damage * arm.getRank());
			}
		}
	}

	@Override
	public ArrayList<Coordinate> calculateAffectedArea(Army army,
			Scenario scenario) {
		ArrayList<Coordinate> temp = new ArrayList<Coordinate>();
		Coordinate target = this.getTarget();
		if (scenario.getBattleMap().isValidCoordinate(target)) {
			temp.add(target);
		}
		return temp;
	}
}
